package com.yjy.fuyao.vod.service.impl;


import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyun.vod20170321.Client;
import com.aliyun.vod20170321.models.DeleteVideoRequest;
import com.yjy.fuyao.utils.exception.BusinessException;
import com.yjy.fuyao.utils.result.ResponseEnum;
import com.yjy.fuyao.vod.service.VodService;
import com.yjy.fuyao.vod.util.AliyunVodSDKUtils;
import com.yjy.fuyao.vod.util.VodProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@Slf4j
@Service
public class VodServiceImpl implements VodService {

    @Override
    public UploadStreamResponse uploadVideo(MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            String fileName = file.getOriginalFilename();
            String title = fileName.substring(0,fileName.lastIndexOf("."));
            UploadStreamResponse response = this.videoUploadStream(VodProperties.KEY_ID, VodProperties.KEY_SECRET, title, fileName, inputStream);
            return response;
        } catch (IOException e) {
            throw new BusinessException(ResponseEnum.ERROR,e);
        }
    }

    @Override
    public void vodDeleteVideo(String keyId,String keySecret,String videoId){
        try {
            Client client = AliyunVodSDKUtils.createClient(keyId, keySecret);
            DeleteVideoRequest deleteVideoRequest = new DeleteVideoRequest();
            deleteVideoRequest.setVideoIds(videoId);
            // 复制代码运行请自行打印 API 的返回值
            client.deleteVideo(deleteVideoRequest);
        } catch (Exception e) {
            throw new BusinessException(ResponseEnum.ERROR,e);
        }
    }

    /**
     * 流式上传接口
     *
     * @param accessKeyId
     * @param accessKeySecret
     * @param title
     * @param fileName
     * @param inputStream
     */
    @Override
    public UploadStreamResponse videoUploadStream(String accessKeyId, String accessKeySecret, String title, String fileName, InputStream inputStream) {
        UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, title, fileName, inputStream);
        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadStreamResponse response = uploader.uploadStream(request);

        //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。
        // 其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
        String videoId = response.getVideoId();
        if (!response.isSuccess()) {
            String errorMessage = "阿里云上传错误：" + "code：" + response.getCode() + ", message：" + response.getMessage();
            log.warn(errorMessage);
            if(StringUtils.isEmpty(videoId)){
                throw new BusinessException(ResponseEnum.ERROR);
            }
        }
        return response;
    }

}
